from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time, random
from MyAutomator import MyAutomation

# Appium 基本参数
PLATFORM = 'Android'
DEVICE_NAME = 'MI_4S'
DRIVER_SERVER = 'http://localhost:4723/wd/hub'
TIMEOUT = 800
NORESET = 'True'
FLICK_STRAT_X = 300
FLICK_START_Y = 300
FLICK_DISTANCE = 700





class Read(MyAutomation):

    def __init__(self, APP_PACKAGE, APP_ACTIVITY):

        # 继续阅读
        self.tryplay_ele = 'com.kmxs.reader:id/bookshelf_book_item_sub_title2'
        super().__init__(APP_PACKAGE, APP_ACTIVITY)








    # 翻页
    def readBook(self, interval):
        # try:
            tryplay = self.wait.until(EC.presence_of_element_located((By.ID, self.tryplay_ele)))
            tryplay.click()
            print('打开 继续阅读')
            start = time.time()
            page = 0
            while True:
                time.sleep(random.randint(3, 7))
                try:
                    self.swipeLeft()
                except:
                    time.sleep(random.randint(3, 7))
                    self.swipeLeft()
                page += 1
                print('翻页 '+str(page))
                end = start + interval
                now = time.time()
                if now >= end:
                    print('结束循环')
                    break
        # except:
        #     print('打开失败')





    # 入口函数
    def main(self):
        print('打开 {} App ...'.format('读'))
        self.readBook(random.randint(20000, 30000))



#qimao
qutoutiao_appPackage = 'com.kmxs.reader'
qutoutiao_appActivity = 'com.km.app.home.view.LoadingActivity'


if __name__ == '__main__':
    qutoutiao = Read(qutoutiao_appPackage, qutoutiao_appActivity)
    qutoutiao.main()